<?
//include('protect.php');     
include('conn.php');
include('top.php');
if (isset($_REQUEST['submit']))
{
//	mysqli_query("invoiceapp_detl(1001)");
	$sql = "Select * from processmast where status = 0 and Batch = '".$_REQUEST['Batch']."'";
	$res=mysql_query($sql) or die(mysql_error());
	if(mysql_num_rows($res) > 0)
	{
		?><script>alert('Batch is Already Processed');</script><?
		echo "<script>window.location.href = 'spprocess.php'</script>";
	}
	$res=mysql_fetch_assoc($res);
	$Batch = $_REQUEST['Batch'];
mysql_query('START TRANSACTION');
try{
	$sql1 = "insert into processmast(Batch, processdate, status) values('".$_REQUEST['Batch']."',sysdate(),0)";
	$res1 = mysql_query($sql1) or die(mysql_error());
	$sqlmast = "Select distinct B.custid from customermast B, invoice3 A where A.`Pax Name` = B.custdesc and A.Batch = '".$Batch."'";
	$sqlm = mysql_query($sqlmast);
	$resm = mysql_fetch_assoc($sqlm);
	$cid = $resm['custid'];
	$sql = "Call invoiceapp_trans(".$_REQUEST['Batch'].",".$cid.")";
//	$res=mysql_query($sql) or die(mysql_error());
    if(!mysql_query($sql))
	{
		echo "Transaction Failed for Master";
        throw new Exception();
	}
	mysql_query('COMMIT');
}
catch(Exception $e){
    mysql_query('ROLLBACK');
	echo "Transaction Rollbacked";
}
if ($cid > 0)
{
	mysql_query('START TRANSACTION');
	try{
		$sql1 = "select distinct `ID Number` from invoice3 where Batch = '".$Batch."'";
		$sqlres = mysql_query($sql1);
		while ($res1 = mysql_fetch_assoc($sqlres))
		{
			$idno = $res1['ID Number'];
			$sql = "Call invoiceapp_detl('$idno','$Batch')";
			if(!mysql_query($sql))
			{
				echo "Transaction Failed for Transaction";
				throw new Exception();
			}
		}
		$sql="update invoice3 set uploaddate = '".$_REQUEST['dateselect']."' WHERE Batch = '".$_REQUEST['Batch']."'";
		mysql_query("SET sql_safe_updates=0");	
	//	$res=mysql_query($sql) or die(mysql_error());
		if(!mysql_query($sql))
		{
			echo "Update Failed";
			throw new Exception();
		}
		mysql_query('COMMIT');
		?><script>alert('Batch Processing Complete');</script><?
	//	echo "<script>window.location.href = 'spprocess.php'</script>";
	}
	catch(Exception $e){
		mysql_query('ROLLBACK');
		echo "Transaction Rollbacked";
	}
 }
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script src="jquery.validate.js"></script>

<script>
$(document).ready(function() {
 
	var formvalidator = $("#frm_up").validate({
		onkeyup:false,
		onfocusout: false,
		onclick: false,
		focusInvalid: false,
		rules: { 
			file: {
				required:true
				},
			dateselect: {
				required: true
				},
		},
		messages: {
			dateselect: {
				required: "Please Select a Date"
				},
		}
	}); 
	$( "#dateselect" ).datepicker({ dateFormat: "yy-mm-dd" });
		date_select=$("#dateselect").val();
});
</script>
<style>
	body {
		font: 12px/16px "Helvetica Neue", Arial, sans-serif;	
	}
	.lines label {
		color: #990000;
		font-size:12px;
		margin-left: 6px;
		float:left !important;
		width:300px;
	}
	.error {
	float:left ;
	}
	.lines input {
		width:200px !important;
	}
	.lines {
		clear:both;
		margin-bottom: 6px;
		height:30px;
	}
	.tit, .lines div {
		float:left;
		
	}
	.tit {
		width:110px !important;
		color: #000000 !important;

	}
</style>
</head>
<body>

	
	<form id="frm_up" name="frm_up" method="POST" enctype="multipart/form-data" action="spprocess.php">
		<input type="hidden" name="task" id="task" value="send_zip" />
<table  cellpadding="3" id="box-table-a" align="center" cellspacing="2" border="1" width="350" bgcolor="#000000" style="background-color:#DFEBF9;">        
		<tr>
        <td>
			<label class="tit" for="dateselect">Select Batch:</label>
            
						<select  name="Batch" id="Batch" class="smallInput" style="width:150px; font-size:12px; font-weight:bold;" >
						<?
							$bs='';
							$sqlb="select distinct Batch from invoice3 order by Batch";
							$resb=mysql_query($sqlb);
							while($rowb=mysql_fetch_assoc($resb))
							{
								if ($rowb['Batch']==$rowedit['Batch'])
									$bs='selected="selected"';
								else
									$bs='';	
						?>
							<option <?=$bs?> value="<?=$rowb['Batch']?>"><?=$rowb['Batch']?></option>
						<?
							}
						?>	
						</select>
					</td>
				</tr>
         <tr>
			<td><label class="tit" for="dateselect">Select Date:</label><input type="text" width="100" name="dateselect" id="dateselect" /></td></tr>
            <tr>
            <td><input type="submit" name="submit" id="submit" style="alignment-adjust:middle"  value="Process Batch"></td></tr>
            </table>
	</form>
</body>
</html>
